import Vue from 'vue'
import {workPointList, getProjectInfo, projectSelect} from '@/services/model/project-info'
import {mutations as Collapse} from '@/views/layout/collapse.store'
import {setLocalStorageItem} from '@/utils/storage-helper'
import {getLocalStorageItem} from "../../utils/storage-helper";

export let store = Vue.observable({

  //3Dmarker是否显示
  isMarker: false,
  //3Dmarker数据
  marker: [],
  // 菜单是否折叠
  isCollapse: false,
  // 工点列表，包含下面的监测点数据
  workPointList: [],
  // 查询项目详细信息
  projectInfo: {},
  // 监测点列表
  // {"code":1,"name":"深部位移"},
  // {"code":2,"name":"地表位移"},
  // {"code":3,"name":"拉线位移"},
  // {"code":4,"name":"降雨"},
  // {"code":5,"name":"地下水位"},
  // {"code":6,"name":"锚索应力"},
  // {"code":7,"name":"静力水准"},
  // {"code":8,"name":"倾角仪"},
  // {"code":9,"name":"拉线拉力"},
  // {"code":10,"name":"温湿度"}]}
  // 工点包含整理归类的监测点菜单数据
  workPoint_MonitorList: [],
  iconMap: {
    1: 'iconshenbuweiyicexieyi',
    2: 'iconGNSS',
    3: 'iconlaxianweiyi',
    4: 'iconjiangyuliang',
    5: 'icondixiashuiwei',
    6: 'iconmaosuochuanganqi',
    7: 'iconjinglishuizhunyi2',
    8: 'iconqingjiao',
    9: 'iconxianxingtu',
    10: 'iconshidu',
    11: 'iconfengsufengxiangjiance',
    12: 'icondiyingli',
    13: 'iconzhendong',
    14: 'iconjianshiqicai01',
    15: 'iconGNSS',
    16: 'iconjianshiqicai01',
    17: 'iconzhendong',
    18: 'iconleida',
  }
})
export let mutations = {
  // 设置数据
  setState({key, value}) {
    store[key] = value
  },
}
export let actions = {
  // 工点列表，包含下面的监测点数据
  async queryWorkPointList(id, index, type) {
    const {data} = await workPointList({id})
    if (type === "综合绘图") {
      return data
    }
    mutations.setState({key: 'workPointList', value: data || []})
    // 设置菜单
    let menu = [
      // {
      //   id: `/project/${id}/probablyEchart`,
      //   menuIcon: 'iconsheji_huatu',
      //   name: '绘图总览',
      //   url: `/project/${id}/probablyEchart`,
      // },
    ]
    let menuComplex = []//综合绘图菜单
    let monitorTypeList = []
    store.workPointList.map(x => {
      const prefix = `/project/${id}/work-point/${x.id}`
      // 数据采集分析下的默认拥有的3个菜单
      const defaultMenu = [
        // {
        //   name: '工点总览',
        //   menuIcon: 'icon-_kanban',
        //   url: prefix,
        //   id: '8978997897897'
        // },
        // {
        //   name: '3D建模',
        //   menuIcon: 'icon-_kanban',
        //   url: `${prefix}/3DModel/1`,
        //   id: '89789978978971'
        // },
        // {
        //   name: '监测点管理',
        //   menuIcon: 'iconjiancedian',
        //   url: `${prefix}/monitor-list`,
        //   id: '987654646767'
        // },
        // {
        //   id: `${prefix}/complex-echart`,
        //   menuIcon: 'icontubiao-zhexiantu',
        //   name: '综合绘图',
        //   url: `${prefix}/complex-echart`,
        // },
        // {
        //   name: '工点级控制标准',
        //   menuIcon: 'iconshebeiyunweiyujingshezhi',
        //   url: `${prefix}/warning-rule`,
        //   id: '987654646'
        // },
        // {
        //   name: '新建监测点',
        //   menuIcon: 'iconjiancedianguanli',
        //   url: `${prefix}/monitor-add`,
        //   id: '654321348'
        // },
        // {
        //   name: '综合绘图',
        //   menuIcon: 'iconjiancedianguanli',
        //   url: `${prefix}/complex-echart`,
        //   id: '560038384'
        // },
      ]
      let parentComplex = {
        name: x.name,
        id: x.id,
        menuIcon: 'icontakan',
        children: []
      }
      let parent = {
        name: x.name,
        id: x.id,
        menuIcon: 'icontakan',
        children: [...defaultMenu]
      }
      x.monitorPointList.map(k => {
        let child = {
          name: k.monitorTypeName,
          menuIcon: store.iconMap[k.monitorType],
          url: `${prefix}/monitor/${k.monitorType}`,
          monitorType: k.monitorType,
          monitorName: k.name,
          id: k.id,
          status: k.status
        }
        let item = parent.children.find(w => w.monitorType === child.monitorType)
        if (item) {
          item.brother.push(child)
        } else {
          child.brother = [{...child}]
          monitorTypeList.push(child)  //传给综合图
          parent.children.push(child)
          parentComplex.children.push(child)//传给综合图
        }
      })
      setLocalStorageItem("monitorTypeList", monitorTypeList || [], 1)
      menu.push(parent)
      menuComplex.push(parentComplex)
    })
    setLocalStorageItem("menuComplexList", menuComplex || [], 1)
    let list = [
      {
        id: `/project/${id}/home`,
        menuIcon: 'icon-_kanban',
        name: '项目统计',
        url: `/project/${id}/home`
      },

      {
        id: `/project/${id}/warning-rule`,
        menuIcon: 'icontiaoshi',
        name: '预警规则',
        url: `/project/${id}/warning-rule`
      },
      {
        id: `/project/${id}/warning-phone`,
        menuIcon: 'icontongzhizhongxin',
        name: '预警通知',
        url: `/project/${id}/warning-phone`
      },
      {
        id: `/project/${id}/info-mgmt`,
        menuIcon: 'iconxiaoxiliebiao',
        name: '预警消息',
        url: `/project/${id}/info-mgmt`
      },
      {
        id: `/project/${id}/work-point`,
        menuIcon: 'icontakan',
        name: '监测对象',
        url: `/project/${id}/work-point`
      },
      {
        name: '监测点位',
        menuIcon: 'iconjiancedian',
        url: `/project/${id}/monitor-list`,
        id: '987654646767'
      },
      {
        id: `/project/${id}/data-share`,
        menuIcon: 'iconshujukanban',
        name: '数据分析',
        url: `/project/${id}/data-share`
        // children: menu
      }

    ]
    Collapse.setState({key: 'sideMenuList', value: list})
    Collapse.setSessionStorage(list)
    setLocalStorageItem("sideMenuList", list || [], 1)
    mutations.setState({key: 'workPoint_MonitorList', value: menu})
    setLocalStorageItem("workPoint_MonitorList", menu || [], 1)
    return list[0].url
  },
  // 查询项目详细信息
  async queryProjectInfo(id) {
    const {data} = await getProjectInfo({id})
    mutations.setState({key: 'projectInfo', value: data || []})
    setLocalStorageItem("projectInfo", data || [], 1)
    let res = await projectSelect()
    setLocalStorageItem("projectSelectList", res.data || [], 1)
  },

}
